(function(){
	$.extend($.fn,{
		regionsSelect : function() {
			var $that_ = $(this);
			var options_ = {
				value: 0,
				minType: 1,
				isLoadParent: true,
				isShowNull: false,
				nullName: "-选择地区-",
				onLoad: function($select){},
				onSelect: function($select){}
			};
			
			if (arguments.length > 0) {
				var options = arguments[0];
				for (var key in options) {
					if (options_[key] !== undefined) {
						options_[key] = options[key];
					}
				}
			}
			
			var cacheData_ = {};
			var containerId_ = "selectRegion_" + $that_.attr("id") + "_";
			// 加载兄弟地区请求地址
			var loadBrothersPath_ = g_domain + "/region/listBrothers/json";
			//地区列表加载地址
			var loadSonsPath_ = g_domain + "/region/listByParentId/json";
			
			
			//加载兄弟地区
			var loadBrothers_ = function(regionId, isPrepend){
				var vars = {
					regionId: regionId
				};
				
				var cachekey = $.param({path: loadBrothersPath_, vars: vars});
				var cacheData = storage_(cachekey);
				if (cacheData) {
					makeHtml_($.parseJSON(cacheData), regionId, isPrepend);
					return;
				}
				
				$.ajax({
					url: loadBrothersPath_,
					data: vars,
					dataType: 'text',
					async: false,
					success: function(data, textStatus, jqXHR) {
						storage_(cachekey, data);
						makeHtml_($.parseJSON(data), regionId, isPrepend);
					}
				});
				
			};
			
			//加载子一级地区
			var loadSons_ = function(parentId, value) {
				var vars = {
					parentId:parentId
				};

				var cachekey = $.param({path: loadSonsPath_, vars: vars});
				var cacheData = storage_(cachekey);
				if (cacheData) {
					makeHtml_($.parseJSON(cacheData), value);
					return;
				}
				
				$.ajax({
					url: loadSonsPath_,
					data: vars,
					dataType: 'text',
					async: false,
					success: function(data, textStatus, jqXHR) {
						storage_(cachekey, data);
						makeHtml_($.parseJSON(data), value);
					}
				});
				
			};
			
			var makeHtml_ = function(data, value, isPrepend) {
				if (data.status !== 0) {
					alert(data.message);
					return;
				}
				var list = data.result;
				if (!list || list.length === 0) {
					return;
				}
				var parentId = list[0].parentId;
				var type = parseInt(list[0].type);
				var selectId = containerId_ + type;
				var $select = $("#" + selectId);
				if ($select.length === 0) {
					if (isPrepend) {
						$that_.prepend('<select id="' + selectId + '"></select>');
					} else {
						$that_.append('<select id="' + selectId + '"></select>');
					}
					$select = $("#" + selectId);
					
				} else {
					$select.html("");
				}
				
				if (options_.isShowNull) {
					$select.append("<option value=''>" + options_.nullName + "</option>");
				}
				
				for (var i = 0; i< list.length; i++) {
					var region = list[i];
					$select.append("<option value='" + region.regionId + "'>" + region.name + "</option>");
				}
				
				// 当前列表有选中值,但下一级没有加载,则加载下一级
				if (value && $("#" + containerId_ + (type + 1)).length === 0) {
					loadSons_(value);
				}
				if (options_.isLoadParent && type > options_.minType && $("#" + containerId_ + (type - 1)).length === 0) {
					loadBrothers_(parentId, true);
				}
				// 选中值
				$select.val(value);
				options_.onSelect($select);
				options_.onLoad($select);
			};
			
			var storage_ = function(key, value) {
				if (!localStorage) {
					return null;
				}
				if (value) {
					return localStorage.setItem(key, value);
				} else {
					return localStorage.getItem(key);
				}
			};
			
			//事件：选择地区
			$(document).off("change", "[id^='" + containerId_ + "']");
			$(document).on("change", "[id^='" + containerId_ + "']",function() {
				var idSplits = $(this).attr("id").split("_");
				var type = parseInt(idSplits[idSplits.length -1]);
				$("[id^='" + containerId_ + "']").each(function(i, el) {
					idSplits = $(this).attr("id").split("_");
					var type2 = parseInt(idSplits[idSplits.length -1]);
					if (type2 > type) {
						$(this).remove();
					}
				});
				if (options_.onSelect) {
					options_.onSelect($(this));
				}
				if ($(this).val()) {
					loadSons_($(this).val());
				}
				
			});
			
			if (options_.value) {
				loadBrothers_(options_.value, true);
			} else {
				loadSons_(1);
			}
		}
	});
})();
